<template>
  <div class="goods-sku" v-if="goods">
    <dl v-for="item in goods.specs" :key="item.id">
      <dt>{{ item.name }}</dt>
      <dd>
        <template v-for="val in item.values" :key="val.name">
          <img
            v-if="val.picture"
            :class="{ selected: val.selected, disabled: val.disabled }"
            @click="changeSelectedStatus(item, val)"
            :src="val.picture"
            :title="val.name"
          />
          <span
            v-else
            :class="{ selected: val.selected, disabled: val.disabled }"
            @click="changeSelectedStatus(item, val)"
            >{{ val.name }}</span
          >
        </template>
      </dd>
    </dl>
  </div>
</template>
<script setup>
import { onMounted, ref } from 'vue'
import axios from 'axios'
import powerSet from './power-set.js'

const goods = ref()
let pathMap = {}
const getGoods = async () => {
  //1135076无库存规格
  //1369155859933827074
  const res = await axios.get(
    'http://pcapi-xiaotuxian-front-devtest.itheima.net/goods?id=1369155859933827074',
  )
  goods.value = res.data.result
  pathMap = getPathMap(goods.value)
  initDosabledStatus(goods.value.specs, pathMap)
}

onMounted(() => {
  getGoods()
})
//切换选中状态
const changeSelectedStatus = (item, val) => {
  if (val.disabled) {
    return
  }
  //item：同一排对象,val：当前点击项
  if (val.selected) {
    val.selected = false
  } else {
    item.values.forEach((val) => (val.selected = false))
    val.selected = true
  }
  updateDisabledStatus(goods.value.specs, pathMap)
  //产出sku对象
  const index = getSelectedValues(goods.value.specs).findIndex((item) => item === undefined)
  if (index > -1) {
  } else {
    const key = getSelectedValues(goods.value.specs).join('-')
    const skuIds = pathMap[key]
    const skuObj = goods.value.skus.find((item) => item.id === skuIds[0])
  }
}
//生成有效路径字典对象
const getPathMap = (goods) => {
  const pathMap = {}
  // 	1.根据库存字段得到有效的sku数组
  const effectiveSkus = goods.skus.filter((sku) => sku.inventory > 0)
  // 2.根据有效的sku数组使用powerSet算法得到所有子集
  effectiveSkus.forEach((sku) => {
    //2.1获取匹配的valueName组成的数组
    const selectedValArr = sku.specs.map((val) => val.valueName)
    //2.2使用算法获取子集
    const valueNamePowerSet = powerSet(selectedValArr)
    // 3.根据子集生成路径字典对象
    valueNamePowerSet.forEach((arr) => {
      //初始化key
      const key = arr.join('-')
      //如果已经存在当前key就往数组中直接添加skuId，如果不存在直接做赋值
      if (pathMap[key]) {
        pathMap[key].push(sku.id)
      } else {
        pathMap[key] = [sku.id]
      }
    })
  })
  return pathMap
}
//初始化禁用状态
const initDosabledStatus = (specs, pathMap) => {
  specs.forEach((spe) => {
    spe.values.forEach((val) => {
      if (pathMap[val.name]) {
        val.disabled = false
      } else {
        val.disabled = true
      }
    })
  })
}
//获取相中项的数组
const getSelectedValues = (specs) => {
  const arr = []
  specs.forEach((spe) => {
    //找到values中selected为true的项，然后把它的name字段添加到对应的位置
    const selectedVal = spe.values.find((item) => item.selected)
    arr.push(selectedVal ? selectedVal.name : undefined)
  })
  return arr
}
//切换时更新禁用状态
const updateDisabledStatus = (specs, pathMap) => {
  specs.forEach((spe, index) => {
    const selectedValues = getSelectedValues(specs)
    spe.values.forEach((val) => {
      selectedValues[index] = val.name
      const key = selectedValues.filter((value) => value).join('-')
      if (pathMap[key]) {
        val.disabled = false
      } else {
        val.disabled = true
      }
    })
  })
}
</script>

<style scoped lang="scss">
@mixin sku-state-mixin {
  border: 1px solid #e4e4e4;
  margin-right: 10px;
  cursor: pointer;

  &.selected {
    border-color: #27ba9b;
  }

  &.disabled {
    opacity: 0.6;
    border-style: dashed;
    cursor: not-allowed;
  }
}

.goods-sku {
  padding-left: 10px;
  padding-top: 20px;

  dl {
    display: flex;
    padding-bottom: 20px;
    align-items: center;

    dt {
      width: 50px;
      color: #999;
    }

    dd {
      flex: 1;
      color: #666;

      > img {
        width: 50px;
        height: 50px;
        margin-bottom: 4px;
        @include sku-state-mixin;
      }

      > span {
        display: inline-block;
        height: 30px;
        line-height: 28px;
        padding: 0 20px;
        margin-bottom: 4px;
        @include sku-state-mixin;
      }
    }
  }
}
</style>
